package com.atguigu.cloud.controller;

import com.atguigu.cloud.entities.Pay;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.rest.ResultData;
import com.atguigu.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author HanYiMing
 * @description
 * @date 2024/3/19
 * @lastEditDate 2024/3/19
 * @lastEditAuthor
 * @lastDescription
 */
@RestController
@Slf4j
@Tag(name = "支付微服务模块", description = "支付DRUD")
public class PayController {
    @Resource
    private PayService payService;

    @PostMapping("/pay/add")
    @Operation(summary = "添加方法", description = "添加支付流水方法")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        int add = payService.add(pay);
        return ResultData.success("成功插入记录，返回值：" + add);
    }

    @DeleteMapping("/pay/del/{id}")
    @Operation(summary = "删除方法", description = "根据id删除支付流水方法")
    public ResultData<Integer> delete(@PathVariable("id") Integer id) {
        return ResultData.success(payService.delete(id));
    }

    @PutMapping("/pay/update")
    @Operation(summary = "修改方法", description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        int update = payService.update(pay);
        return ResultData.success("成功修改记录，返回值：" + update);
    }

    @GetMapping("/pay/getById/{id}")
    @Operation(summary = "查询方法", description = "根据支付流水id查询单个支付流水信息")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        if (id == -4) {
            throw new RuntimeException("id不能为负数");
        }
        return ResultData.success(payService.getById(id));
    }

    @GetMapping("/pay/getAll")
    @Operation(summary = "查询方法", description = "查询全部支付流水方法")
    public ResultData<List<Pay>> getAll() {
        return ResultData.success(payService.getAll());
    }

    @Value("${server.port}")
    private String port;

    @GetMapping(value = "pay/get/info")
    public String getInfoByConsul(@Value("${atguigu.info}") String atguiguInfo) {
        return "atguiguInfo:" + atguiguInfo + "\t" + "port:" + port;
    }


}
